package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.supports.PageData;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.CerSysteminitImportInfoService;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/v1/cerSysteminitImportInfo/")
public class CerSysteminitImportInfoController extends BaseController {

    @Autowired
    private CerSysteminitImportInfoService cerSysteminitImportInfoService;

    @PostMapping("listAllTableData")
    @ApiOperation("获取所有的初始数据信息列表")
    public JsonObj listByPage() {
        return cerSysteminitImportInfoService.listAllTableData(getPageData());
    }

    @PostMapping("batchDeletion")
    @ApiOperation("批量删除")
    public JsonObj batchDeletion() {
        PageData pageData = getPageData();
        return cerSysteminitImportInfoService.batchDeletion(pageData);
    }

    @GetMapping("oneClickDelete")
    @ApiOperation("一键删除")
    public JsonObj oneClickDelete() {
        PageData pageData = getPageData();
        return cerSysteminitImportInfoService.oneClickDelete(pageData);
    }

    @PostMapping("exportSysteminitImportInfo")
    @ApiOperation("导出初始数据")
    public void exportSysteminitImportInfo(HttpServletResponse response) {
        PageData pageData = getPageData();

        // 查询出可以用来导出的数据源
        JsonObj jsonObj = cerSysteminitImportInfoService.queryAllExportData(pageData);
        List<LinkedHashMap<String, Object>> dataList = (List<LinkedHashMap<String, Object>>) jsonObj
            .getData()
            .get("data");

        // 组装表头
        List<String> headList = Arrays.asList("姓名",
                                              "性别",
                                              "年龄",
                                              "身份证号",
                                              "地址",
                                              "联系电话",
                                              "门诊号",
                                              "住院号",
                                              "内镜诊疗日期",
                                              "对象来源",
                                              "内镜号",
                                              "是否无痛",
                                              "染色类别",
                                              "图像数量",
                                              "是否活检",
                                              "Hp检测结果",
                                              "Hp检测方法",
                                              "内镜诊断",
                                              "内镜诊断医师",
                                              "内镜审核医师",
                                              "病理号",
                                              "病理诊断",
                                              "食管病理诊断代码",
                                              "贲门病理诊断代码",
                                              "胃病理诊断代码",
                                              "十二指肠病理诊断代码",
                                              "病理诊断医师",
                                              "病理审核医师",
                                              "病理报告日期");

        try {
            // 导出
            ExcelUtils.exportExcel(headList, dataList, "初始数据导入", response);
        } catch (Exception e) {
        }
    }
}
